﻿//主界面
package com.victim.snake.graphics
{
	import flash.display.Sprite;
	import fl.controls.Button;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;	
	import fl.controls.List;

	
	import com.victim.snake.user.User;
	import com.victim.snake.user.ProcessMsg;
	import com.victim.snake.graphics.RoomSprite;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.events.ListEvent;
	import com.victim.snake.util.Loader;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import flash.display.Bitmap;
	import com.victim.snake.map.Map;
	import com.victim.snake.map.MapEvent;
	import com.victim.snake.util.GraphicsToll;
	import com.victim.snake.graphics.mainClass.RoomGrid;
	import com.victim.snake.graphics.mainClass.Chat;
	import com.victim.snake.room.Room;

	//import com.victim.snake.graphics.mainClass.RoomListSprite;
	
	//游戏主界面
	public class MainSprite extends Sprite
	{
		//var roomList:DataGrid =null;
		//var butJoin:Button=null;
		var butNewRoom:Button=null;
		var preBtn:Button=null;
		var nextBtn:Button=null;
		public var createRoomSprite:CreateRoomSprite=null;
		var roomGrid:RoomGrid=null;	//房间列表
		public var chat:Chat=null;
		
		private var roomlistCurrentPage:int=0;	//当前页
		private var nextRoomlistCurrentPage:int=0;	//当前页
		private var roomItemNum:int=0;	//当前房间列表数
		
		
		public function MainSprite()
		{	
			//下载主页面背景图片
			var loader:Loader = new Loader("image/main/mainBack.jpg",this.mainBackLoaderEvent);
			var loaderNewRoomBtn:Loader = new Loader("image/main/newRoomBtn.png",this.newRoomBtnLoaderEvent);
			var preBtnLoad:Loader = new Loader("image/main/preBtn.jpg",this.preBtnLoaderEvent);
			var preBtnOverLoad:Loader = new Loader("image/main/preBtnOver.jpg",this.preBtnOverLoaderEvent);
			var nextBtnLoad:Loader = new Loader("image/main/nextBtn.jpg",this.nextBtnLoaderEvent);
			var nextBtnOverLoad:Loader = new Loader("image/main/nextBtnOver.jpg",this.nextBtnOverLoaderEvent);
			
			
			/*dataGride房间列表，注释于2013-03-02 因为添加房间界面，将接收的房间数剧显示为每个room
			//创建房间列表
			roomList=new DataGrid();
			roomList.editable=false;			
			roomList.x=40;
			roomList.y=40;
			roomList.width=620;
			roomList.height=300;
			
			var dataGridColumns:Array=new Array();
			
			var dgc:DataGridColumn = new DataGridColumn("房号");
			dgc.sortable=false;
			dgc.editable=false;
			dgc.dataField="roomId";
			dgc.width=50;
			dataGridColumns[0]=dgc;
			
			dgc = new DataGridColumn("房间名称");
			dgc.sortable=false;
			dgc.editable=false;
			dgc.dataField="roomTitle";
			dgc.width=400;
			dataGridColumns[1]=dgc;
			
			dgc = new DataGridColumn("玩家数");
			//dgc.sortable=true;
			dgc.editable=false;
			dgc.dataField="players";
			dgc.width=50;
			//dgc.sortOptions=Array.NUMERIC|Array.DESCENDING;
			dataGridColumns[2]=dgc;
			
			dgc = new DataGridColumn("房主");
			dgc.sortable=false;
			dgc.editable=false;
			dgc.dataField="master";
			dgc.width=120;
			dataGridColumns[3]=dgc;
			
			roomList.columns=dataGridColumns;			
			roomList.addEventListener(ListEvent.ITEM_CLICK,itemClick);	//单击列表项事件			
			roomList.visible=false;
			this.addChildAt(roomList,0);
			
			*/
			
			//房间列表上一页
			preBtn=new Button();
			preBtn.width=43;
			preBtn.height=13;
			preBtn.x=447;
			preBtn.y=303;
			preBtn.addEventListener(MouseEvent.CLICK,preBtnClick);
			this.addChildAt(preBtn,0);
			
			//房间列表下一页
			nextBtn=new Button();
			nextBtn.width=43;
			nextBtn.height=13;
			nextBtn.x=500;
			nextBtn.y=303;
			nextBtn.addEventListener(MouseEvent.CLICK,nextBtnClick);
			this.addChildAt(nextBtn,0);
		
			//开设房间按钮
			butNewRoom=new Button();
			//butNewRoom.label="创建房间";
			butNewRoom.x=234;
			butNewRoom.y=52;
			butNewRoom.addEventListener(MouseEvent.CLICK,createRoom);
			this.addChildAt(butNewRoom,0);
			
			//加入房间按钮
			/*butJoin=new Button();
			butJoin.label="加入";
			butJoin.x=220;
			butJoin.y=360;
			butJoin.mouseEnabled=false;
			butJoin.addEventListener(MouseEvent.CLICK,joinRoom);
			this.addChildAt(butJoin,1);
			*/
			//创建房间对话框
			createRoomSprite=new CreateRoomSprite();
			//createRoomSprite.visible=false;
			//this.addChildAt(createRoomSprite,2);	//添加创建房间对话框
			
			
			
			
			//创建房间列表
			roomGrid=new RoomGrid();
			this.addChildAt(roomGrid,0);
			//向服务器请求房间列表数据
			this.requestRoomList(this.roomlistCurrentPage);
			
			//聊天控件
			this.chat=new Chat(160);
			this.chat.x=214;
			this.chat.y=341;
			this.addChild(chat);
			User.setChatSprite(chat);
			
		}
		
		//创建房间按钮图片加载
		public function newRoomBtnLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			butNewRoom.setStyle("icon",Bitmap(loaderInfo.content));
		}
		
		//房间列表上一页按钮
		public function preBtnLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			preBtn.setStyle("icon",Bitmap(loaderInfo.content));
		}
		
		//当鼠标移到房间列表上一页按钮上
		public function preBtnOverLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			preBtn.setStyle("overIcon",Bitmap(loaderInfo.content));
		}
		
		//房间列表下一页按钮
		public function nextBtnLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			nextBtn.setStyle("icon",Bitmap(loaderInfo.content));
		}
		
		//当鼠标移到房间列表下一页按钮上
		public function nextBtnOverLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			nextBtn.setStyle("overIcon",Bitmap(loaderInfo.content));			
		}
		
		//主界面背景画面加载
		public function mainBackLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.addChildAt(Bitmap(loaderInfo.content),0);
		}
		
		
		/*public function JoinBtnMouseEnabled(bol:Boolean){
			this.butJoin.mouseEnabled=bol;
		}*/
		
/*		注释于2013-03-02  同房间列表dataGride一起注释
		//单击列表项
		private function itemClick(event:ListEvent):void{
			//trace(event.item.roomId);
		}
		*/
		
		//点击创始房间按钮
		private function createRoom(event:MouseEvent):void
		{
			//trace(1);
			this.createRoomSprite.init();
			this.createRoomSprite.visible=true;
			User.getStage().focus=this.createRoomSprite.roomName;
			this.addChildAt(createRoomSprite,this.numChildren);	//添加创建房间对话框
		}
		
		//点击上一页按钮
		private function preBtnClick(event:MouseEvent):void
		{
			if(this.roomlistCurrentPage>0){
				nextRoomlistCurrentPage=this.roomlistCurrentPage-1;
				this.requestRoomList(this.roomlistCurrentPage-1);
			}
			
		}
		
		//点击下一页按钮
		private function nextBtnClick(event:MouseEvent):void
		{
			nextRoomlistCurrentPage=this.roomlistCurrentPage+1;
			this.requestRoomList(this.roomlistCurrentPage+1);
		}
		
		public function setPage():void{
			this.roomlistCurrentPage=this.nextRoomlistCurrentPage;
		}
		
		/*//点击加入房间按钮
		private function joinRoom(event:MouseEvent):void
		{
			//发送UTF8类型的房间id到服务器，请求加入房间			
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(6,roomList.selectedItem.roomId));
		}*/
		
		//向服务器请求房间列表
		public function requestRoomList(i:int):void{
			
/*			注释于2013-03-02  同房间列表dataGride一起注释
			//请求房间列表
			this.roomList.removeAll();*/

			
			//按页数取得房间列表
			//trace("请求房间数据");
			this.roomGrid.setLoadingState(true);
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(4,i));
		}
		

		
		//添加房间列表项
		public function addRoomItem(room:Room):void{
			//trace("添加房间列表项");
			this.roomGrid.addItem(room);
		}
		
		
		
		//初使化主界面
		public function init():void{
			//this.removeChild(createRoomSprite);
			this.createRoomSprite.visible=false;
			this.roomGrid.init();
			this.requestRoomList(this.roomlistCurrentPage);
			User.setChatSprite(this.chat);
		}

	}

}